using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Data.OleDb;

namespace TCCT_CTC_CSDLDangVienV1
{
    public static class OleDBClass
    {
        #region ConnectionString

        private static string _connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "config.mdb;Jet OLEDB:Database Password=135792;";

        /// <summary>
        /// Gets or sets the connection string.
        /// </summary>
        /// <value>The connection string.</value>
        public static string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        #endregion ConnectionString

        #region OleDbConnection


        private static OleDbConnection _oleConn = null;

        /// <summary>
        /// Gets or sets the OLE conn.
        /// </summary>
        /// <value>The OLE conn.</value>
        public static OleDbConnection OleConn
        {
            get
            {
                if (_oleConn == null)
                    _oleConn = new OleDbConnection(ConnectionString);

                return _oleConn;
            }
            set { _oleConn = value; }
        }

        #endregion OleDbConnection


        public static DataTable OleReturnDataTable(string sql)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand(sql, OleConn);
                cmd.CommandTimeout = 10000;
                if (OleConn.State != ConnectionState.Open)
                    OleConn.Open();
                DataTable dt = new DataTable();
                OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
                adapter.Fill(dt);
                OleConn.Close();
                return dt;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void OleExecuteNonQuery(string sql)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand(sql, OleConn);
                if (OleConn.State != ConnectionState.Open)
                    OleConn.Open();
                cmd.ExecuteNonQuery();
                OleConn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static object OleExecuteScalar(string sql)
        {
            try
            {
                OleDbCommand cmd = new OleDbCommand(sql, OleConn);
                if (OleConn.State != ConnectionState.Open)
                    OleConn.Open();
                return cmd.ExecuteScalar();
                OleConn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
